# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os

num = 0


class BolesPipeline(object):
    '''
    功能：保存item数据
    '''

    def __init__(self):
        self.path = 'data/'
        if not os.path.exists(self.path):
            os.mkdir(self.path)

    def process_item(self, item, spider):
        global num
        num += 1
        self.file = open(self.path + str(num) + '、' + str(item['title']) + '.txt', 'w')
        self.file.write('标题：' + item['title'] + '\n')
        self.file.write('发表日期：' + item['date'] + '\n')
        self.file.write('文章内容：' + '\n')
        for entry in item['entry']:
            self.file.write(entry + '\n')
        self.file.write(item['url'] + '\n')
        return item

    def close_spider(self, spider):
        self.file.close()
